package projectManagement;

import java.awt.Toolkit;
import java.io.Serializable;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.*;

public class Editor extends JEditorPane implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 355429422445915698L;
	StyledDocument doc;
	String text;
	int start;
	
	public Editor(){
		this.setEditorKit(new StyledEditorKit());
        this.initListener();
	}
	
	private void initListener() {
        getDocument().addDocumentListener(new DocumentListener(){
        	
            @Override
			public void insertUpdate(DocumentEvent event) {
                final DocumentEvent e=event;
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
					public void run() {
                        if (e.getDocument() instanceof StyledDocument) {
                            try {                 	
                            		doc =	(StyledDocument)e.getDocument();
                            		start = 0;
	                                //start = Utilities.getRowStart(Editor.this,Math.max(0,e.getOffset()-1));
	                                int end = Utilities.getWordStart(Editor.this,e.getOffset()+e.getLength());
	                                text = doc.getText(start, end-start);
	                                         
	                                replaceAllEmoticons(text.indexOf("--"), "--", new ImageIcon(Toolkit.getDefaultToolkit().getImage(ClientGUI.class.getResource("/emoticons/1.png"))));
	                                replaceAllEmoticons(text.indexOf(":D"), ":D", new ImageIcon(Toolkit.getDefaultToolkit().getImage(ClientGUI.class.getResource("/emoticons/2.png"))));
	                                replaceAllEmoticons(text.indexOf(":p"), ":p", new ImageIcon(Toolkit.getDefaultToolkit().getImage(ClientGUI.class.getResource("/emoticons/3.png"))));
	                                replaceAllEmoticons(text.indexOf(":)"), ":)", new ImageIcon(Toolkit.getDefaultToolkit().getImage(ClientGUI.class.getResource("/emoticons/4.png"))));
	                                replaceAllEmoticons(text.indexOf("Oo"), "Oo", new ImageIcon(Toolkit.getDefaultToolkit().getImage(ClientGUI.class.getResource("/emoticons/5.png"))));
	                                replaceAllEmoticons(text.indexOf("+-"), "+-", new ImageIcon(Toolkit.getDefaultToolkit().getImage(ClientGUI.class.getResource("/emoticons/6.png"))));
	                                replaceAllEmoticons(text.indexOf("oO"), "oO", new ImageIcon(Toolkit.getDefaultToolkit().getImage(ClientGUI.class.getResource("/emoticons/7.png"))));	                         
	                                replaceAllEmoticons(text.indexOf("=="), "==", new ImageIcon(Toolkit.getDefaultToolkit().getImage(ClientGUI.class.getResource("/emoticons/8.png"))));	                                	                               	                                
	                                replaceAllEmoticons(text.indexOf(":("), ":(", new ImageIcon(Toolkit.getDefaultToolkit().getImage(ClientGUI.class.getResource("/emoticons/9.png"))));
	                                replaceAllEmoticons(text.indexOf(":V"), ":V", new ImageIcon(Toolkit.getDefaultToolkit().getImage(ClientGUI.class.getResource("/emoticons/10.png"))));
	                                replaceAllEmoticons(text.indexOf("OO"), "OO", new ImageIcon(Toolkit.getDefaultToolkit().getImage(ClientGUI.class.getResource("/emoticons/11.png"))));

   
                            } catch (BadLocationException e1) {
                                e1.printStackTrace();
                            }
                        }
                    }
                });
            }
            @Override
			public void removeUpdate(DocumentEvent e) {
            }
            @Override
			public void changedUpdate(DocumentEvent e) {
            }
        });
    }
	
	private void replaceAllEmoticons(int position, String type, ImageIcon emoticon){
		try{
	        while(position>=0) {
	            final SimpleAttributeSet attrs=new SimpleAttributeSet(
	               doc.getCharacterElement(start+position).getAttributes());
	            if (StyleConstants.getIcon(attrs)==null) {
	                StyleConstants.setIcon(attrs, emoticon);
	                doc.remove(start+position, 2);
	                doc.insertString(start+position,type, attrs);
	            }
	            position = text.indexOf(type, position+2);
	        }
		}catch(Exception e){}
		
	}
	
	

}
